using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using ClassBuilderWizard.Interfaces;

namespace ClassBuilderWizard
{
    public partial class frmStartWizard : WizardFormBase, IDisposable
    {
        private Plugin _SelectedPlugin;
        private List<Plugin> _PluginList;

        public frmStartWizard()
        {
            InitializeComponent();
        }

        public Plugin SelectedPlugin
        {
            get { return this._SelectedPlugin; }
        }

        #region Event Handlers
        private void frmStartWizard_Load(object sender, EventArgs e)
        {
            RegistryHelper rh = new RegistryHelper();
            chkShowDetails.Checked = rh.GetShowPluginDetails();
            LoadPlugins();
            ToggleNextButton(false);
        }
        private void lvPlugins_SelectedIndexChanged(object sender, EventArgs e)
        {
            //make sure a plugin is selected before allowing the user to click "Next"
            ListView lv = (ListView)sender;
            ToggleNextButton(lv.SelectedItems.Count == 1);
        }
        private void chkShowDetails_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            ToggleDetailsView(chk.Checked);
        }
        #endregion

        #region Private Methods
        private void ToggleDetailsView(bool showDetails)
        {
            if (showDetails)
                lvPlugins.View = View.Details;
            else
                lvPlugins.View = View.List;
        }
        private void ToggleNextButton(bool enable)
        {
            btnNext.Enabled = enable;
        }
        private void LoadPlugins()
        {
            lvPlugins.Items.Clear();
            ilPluginIcons.Images.Clear();

            PluginHelper ph = new PluginHelper();
            //System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
            //string pluginPath = Path.GetDirectoryName(asm.Location);
            RegistryHelper regHelper = new RegistryHelper();
            string pluginPath = regHelper.GetInstallPath(); // @"C:\Program Files\Jason Williams\Class Builder Wizard\";

            pluginPath += (pluginPath.EndsWith(@"\")) ? string.Empty : @"\";
            pluginPath += "plugins";

            try
            {
                this._PluginList = ph.GetAllPlugins(pluginPath);
                foreach (Plugin plugin in this._PluginList)
                {
                    ilPluginIcons.Images.Add(plugin.Icon);
                    int imageIndex = ilPluginIcons.Images.Count - 1;
                    ListViewItem lvi = new ListViewItem(new string[] { plugin.Name, plugin.Version, plugin.Author });
                    lvi.Tag = plugin;
                    lvi.ImageIndex = imageIndex;

                    lvPlugins.Items.Add(lvi);
                }
            }
            catch
            {
                MessageBox.Show(this, string.Format("An error occured loading plugins from:\r\n{0}", pluginPath), "Error Loading Plugins", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (lvPlugins.SelectedItems.Count > 0)
                Next(lvPlugins.SelectedItems[0]);
        }

        private void lvPlugins_ItemActivate(object sender, EventArgs e)
        {
            Next(lvPlugins.SelectedItems[0]);
        }

        private void Next(ListViewItem lvi)
        {
            this._SelectedPlugin = (Plugin)lvi.Tag;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            if (this._PluginList != null && this._PluginList.Count > 0)
            {
                foreach (Plugin p in this._PluginList)
                    p.Dispose();
                this._PluginList = null;
            }
        }

        #endregion
    }
}